//
//  MNDUserCenterHeaderView.m
//  miniwangdai2
//
//  Created by Steven.Yung on 2017/3/29.
//  Copyright © 2017年 谭谭伟. All rights reserved.
//

#import "MNDUserCenterHeaderView.h"
#import "JXLayoutButton.h"

@interface MNDUserCenterHeaderView ()

//按钮
@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, strong) NSArray *buttonContents;

@property (nonatomic, strong) UIView *centerView;
/** 
 *  资产总额
 */
@property (nonatomic, strong) UILabel *amountLabel;
/**
 *  账户余额
 */
@property (nonatomic, strong) UILabel *balanceLabel;
/**
 *  累计收益
 */
@property (nonatomic, strong) UILabel *earningsLabel;
/**
 *  积分
 */
@property (nonatomic, strong) UILabel *integralsLabel;

/**
 *  用户头像
 */
@property (nonatomic, strong) UIImageView *portrait;

@end

@implementation MNDUserCenterHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init {
    self = [super init];
    
    if (self) {
        [self initialUI];
    }
    
    return self;
}

- (void)initialUI {
    
    self.backgroundColor = [UIColor whiteColor];
    
    UIStackView *bottomView = [[UIStackView alloc] initWithArrangedSubviews:self.buttons];
    bottomView.alignment = UIStackViewAlignmentCenter;
    bottomView.axis = UILayoutConstraintAxisHorizontal;
    bottomView.distribution = UIStackViewDistributionEqualCentering;
    [self addSubview:bottomView];
    
//    [self addSubview:self.centerView];
//    [self addSubview:self.portrait];
//    UIView *gapView = [[UIView alloc] init];
//    gapView.backgroundColor = [UIColor colorWithHex:0xefeff4];
//    [self addSubview:gapView];
//    [gapView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self);
//        make.right.equalTo(self);
//        make.bottom.equalTo(self);
//        make.height.mas_equalTo(10.0);
//    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-25.0);
        make.left.equalTo(self).offset(15.0);
        make.right.equalTo(self).offset(-15.0);
    }];
    
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bottomView.mas_top).offset(-20.0);
        make.left.equalTo(self).offset(10.0);
        make.right.equalTo(self).offset(-10.0);
    }];
    
    [self.portrait mas_makeConstraints:^(MASConstraintMaker *make) {
        UIImage *defaultPortrait = [UIImage imageNamed:@"user_center_portrait_placeholder"];
        make.width.mas_equalTo(defaultPortrait.size.width);
        make.height.mas_equalTo(defaultPortrait.size.height);
        make.centerX.equalTo(self.centerView);
        make.centerY.equalTo(self.centerView.mas_top).offset(8.0);
        make.top.equalTo(self.mas_top).offset(40.0);
    }];
}

- (void)buttonClick:(UIButton *)sender {
    
    
}

#pragma mark - Getter

- (NSMutableArray *)buttons {
    
    if (!_buttons) {
        _buttons = [NSMutableArray array];
        
        for (NSDictionary *dict in self.buttonContents) {
            JXLayoutButton *button = [JXLayoutButton buttonWithType:UIButtonTypeSystem];
            button.layoutStyle = JXLayoutButtonStyleUpImageDownTitle;
            button.titleLabel.font = [UIFont systemFontOfSize:14.0];
            [button setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
            [button setTitle:dict[@"title"] forState:UIControlStateNormal];
            [button setImage:dict[@"image"] forState:UIControlStateNormal];
            
            button.tag = 1000 + [self.buttonContents indexOfObject:dict];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [_buttons addObject:button];
        }
    }
    
    return _buttons;
}

- (NSArray *)buttonContents {
    
    if (!_buttonContents) {
        _buttonContents = @[@{@"title": @"提现", @"image": [UIImage imageNamed:@"user_center_withdraw_cash"]},
                            @{@"title": @"充值", @"image": [UIImage imageNamed:@"user_center_recharge"]},
                            @{@"title": @"出借", @"image": [UIImage imageNamed:@"user_center_tender"]},
                            @{@"title": @"回款", @"image": [UIImage imageNamed:@"user_center_refund"]}];
    }
    
    return _buttonContents;
}

- (UIView *)centerView {
    
    if (!_centerView) {
        _centerView = [[UIView alloc] init];
        
        [self addSubview:_centerView];
        
        UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user_center_background"]];
        [_centerView addSubview:backgroundView];
        
        //文字颜色
        UIColor *textColor = [UIColor whiteColor];  //文字颜色白色
        UIColor *titleTextColor = [UIColor colorWithHex:0xffffff alpha:0.8]; //带透明度的白色
        
        //资产总额
        _amountLabel = [UILabel hm_labelWithText:@"0.00" fontSize:30.0 color:textColor];
        UILabel *amountTitleLabel = [UILabel hm_labelWithText:@"资产总额（元）" fontSize:14.0 color:titleTextColor];
        
        CGFloat spacing = 5.0;
        //账户余额
        _balanceLabel = [UILabel hm_labelWithText:@"0.00" fontSize:16.0 color:textColor];
        UILabel *balanceTitleLabel = [UILabel hm_labelWithText:@"账户余额(元)" fontSize:13.0 color:titleTextColor];
        UIStackView *balanceView = [[UIStackView alloc] initWithArrangedSubviews:@[_balanceLabel, balanceTitleLabel]];
        balanceView.alignment = UIStackViewAlignmentCenter;
        balanceView.axis = UILayoutConstraintAxisVertical;
        balanceView.spacing = spacing;
        
        //累计收益
        _earningsLabel = [UILabel hm_labelWithText:@"0.00" fontSize:16.0 color:textColor];
        UILabel *earningTitleLabel = [UILabel hm_labelWithText:@"累计收益(元)" fontSize:13.0 color:titleTextColor];
        UIStackView *earningsView = [[UIStackView alloc] initWithArrangedSubviews:@[_earningsLabel, earningTitleLabel]];
        earningsView.alignment = UIStackViewAlignmentCenter;
        earningsView.axis = UILayoutConstraintAxisVertical;
        earningsView.spacing = spacing;
        
        
        //积分
        _integralsLabel = [UILabel hm_labelWithText:@"0" fontSize:16.0 color:textColor];
        UILabel *integralsTitleLabel = [UILabel hm_labelWithText:@"积分" fontSize:13.0 color:titleTextColor];
        UIStackView *integralsView = [[UIStackView alloc] initWithArrangedSubviews:@[_integralsLabel, integralsTitleLabel]];
        integralsView.alignment = UIStackViewAlignmentCenter;
        integralsView.axis = UILayoutConstraintAxisVertical;
        integralsView.spacing = spacing;
        
        //左边分割线
        UIView *leftLineView = [[UIView alloc] init];
        leftLineView.backgroundColor = titleTextColor;
        //右边分割线
        UIView *rightLineView = [[UIView alloc] init];
        rightLineView.backgroundColor = titleTextColor;
        
        UIStackView *bottomView = [[UIStackView alloc] initWithArrangedSubviews:@[balanceView, leftLineView, earningsView, rightLineView, integralsView]];
        bottomView.alignment = UIStackViewAlignmentCenter;
        bottomView.axis = UILayoutConstraintAxisHorizontal;
        bottomView.distribution = UIStackViewDistributionEqualCentering;
        bottomView.spacing = 0.0;
        
        //addsubview
        [_centerView addSubview:_amountLabel];
        [_centerView addSubview:amountTitleLabel];
        [_centerView addSubview:bottomView];
        WeakSelf(self);
        [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakself.centerView).width.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
//        backgroundView.backgroundColor = [UIColor blackColor];
        //约束
        [amountTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakself.centerView);
            make.top.equalTo(weakself.centerView.mas_centerY).offset(-10.0);
        }];
        
        [_amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(amountTitleLabel.mas_top).offset(0.0);
            make.centerX.equalTo(weakself.centerView);
        }];
        
        [leftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0.5);
            make.height.mas_equalTo(30.0);
        }];
        
        [rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(leftLineView);
            make.height.equalTo(leftLineView);
        }];
        
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakself.centerView).offset(20.0);
            make.bottom.equalTo(weakself.centerView).offset(-20.0);
            make.right.equalTo(weakself.centerView).offset(-20.0);
        }];
    }
    
    return _centerView;
}

- (UIImageView *)portrait {
    
    if (!_portrait) {
        UIImage *portraitImage = [UIImage imageNamed:@"user_center_portrait_placeholder"];
        _portrait = [[UIImageView alloc] initWithImage:portraitImage];
        _portrait.layer.cornerRadius = portraitImage.size.width / 2;
        _portrait.layer.masksToBounds = YES;
        
        [self addSubview:_portrait];
    }
    
    return _portrait;
}

@end
